﻿using System.Xml.Linq;
using Engine;
using Game;
namespace Mekiasm
{
    public class MekShortInventoryWidget : CanvasWidget
    {


        public GridPanelWidget m_inventoryGrid;
        public IInventory inventory1;
        public MekShortInventoryWidget()
        {
            XElement node = ContentManager.Get<XElement>("Widgets/ShortInventoryWidget");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
        }
        public override void MeasureOverride(Vector2 parentAvailableSize)
        {
            if (inventory1 == null) return;
            int max = (inventory1 is ComponentCreativeInventory) ? 10 : 7;
            inventory1.VisibleSlotsCount = MathUtils.Clamp((int)((parentAvailableSize.X - 320f - 25f) / 72f), 7, max);
            if (inventory1.VisibleSlotsCount != m_inventoryGrid.Children.Count)
            {
                m_inventoryGrid.Children.Clear();
                m_inventoryGrid.RowsCount = 1;
                m_inventoryGrid.ColumnsCount = inventory1.VisibleSlotsCount;
                for (int i = 0; i < m_inventoryGrid.ColumnsCount; i++)
                {
                    InventorySlotWidget inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(inventory1, i);
                    inventorySlotWidget.BevelColor = new Color(181, 172, 154) * 0.6f;
                    inventorySlotWidget.CenterColor = new Color(181, 172, 154) * 0.33f;
                    m_inventoryGrid.Children.Add(inventorySlotWidget);
                    m_inventoryGrid.SetWidgetCell(inventorySlotWidget, new Point2(i, 0));
                }
            }
            base.MeasureOverride(parentAvailableSize);
        }
        public void AssignComponents(IInventory inventory)
        {
            if (inventory != inventory1)
            {
                inventory1 = inventory;
                m_inventoryGrid.Children.Clear();
            }
        }
    }
}
